import React, { Component, Fragment } from "react";
// antd
import { Button, Tag, Tooltip } from "antd";
// table 组件
import Action from "@c/buttonAction/Index";
import TableComponent from "@c/tableData/Index";
import TaskModal from "./components/DynamicModal";

class TaskList extends Component {
    constructor(props){
        super(props);
        this.state = {
            // id
            id: "",
            // flag
            flag: false,
            // 请求参数
            PageNumber: 1,
            PageSize: 10,
            keyWork: "",
            // 表头
            tableConfig: {
                url: "taskList",
                method: "get",
                checkbox: true,
                thead: [
                    { 
                        title: "名称", 
                        dataIndex: "name", 
                        key: "name"
                    },
                    { 
                        title: "API", 
                        dataIndex: "execute", 
                        key: "execute"
                    },
                    { 
                        title: "参数", 
                        dataIndex: "params", 
                        key: "params",
                        render: params => {
                            console.log(params.formItem)
                            return (
                                <div>
                                    {
                                        params.formItem.map(item => (
                                            <Tooltip title={item.name}>
                                                <Tag color={item.required ? 'blue': 'green'}>{item.label}</Tag>
                                            </Tooltip>
                                        ))
                                    }
                                </div>
                            )
                        }
                    },
                    { 
                        title: "权限", 
                        dataIndex: "role", 
                        key: "role"
                    },
                    { 
                        title: "操作", 
                        dataIndex: "operation", 
                        key: "operation", 
                        width: 215,
                        render: (text, rowData) => {
                            return (
                                <div className="inline-button">
                                    <Action>
                                        <Action.Button onClick={() => this.taskModal({status: true, rowData: rowData})}>执行</Action.Button>
                                        <Action.Button onClick={() => this.delete(rowData.id)} disabled>编辑</Action.Button>
                                        <Action.Button onClick={() => this.delete(rowData.id)} disabled>删除</Action.Button>
                                    </Action>
                                </div>
                            )
                        }
                    }
                ],
                formItem: [
                    { 
                        type: "Input",
                        label: "名称", 
                        name: "name", 
                        placeholder: "请输入流程名称"
                    }
                ],
                formSearchCol: 18,
                formRightCol: 6,
            },
            // 表的数据
            data: []
        };
    }
    /** 生命周期挂载完成 */
    componentDidMount(){}

    // 获取子组件实例
    getChildRef = (ref) => {
        this.tableComponent = ref; // 存储子组件
    }
    // 获取弹窗子组件实例
    getTaskModalRef = (ref) => { this.child = ref }
    // 显示弹窗
    taskModal = (data) => {
        console.log(data)
        this.child.visibleModal(data);
    }
    /** 删除 */
    delete = (id) => {
        this.tableComponent.onHandlerDelete(id)
    }
    
    render(){
        return (
            <Fragment>
                <TableComponent onRef={this.getChildRef} batchButton={true} config={this.state.tableConfig}>
                    <Button type="primary" ref="taskAdd" onClick={() => this.taskModal({ status: true })}>添加</Button>
                </TableComponent>
                <TaskModal onRef={this.getTaskModalRef} />
            </Fragment>
        )
    }
}
export default TaskList;